package com.kuang.demo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.demo.bean.GoodsBean.GoodsBO.AddGoods;
import com.kuang.demo.bean.GoodsBean.GoodsVO.GoodsDetailVO;
import com.kuang.demo.bean.acgBeanVo.BaseData;
import com.kuang.demo.bean.lhf.*;
import com.kuang.demo.mapper.AddGoodsMapper;
import com.kuang.demo.mapper.GoodsMapper;
import com.kuang.demo.mapper.GrouponMapper;
import com.kuang.demo.mapper.GrouponRulesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GrouponServiceImp implements GrouponService {

    @Autowired
    GrouponMapper grouponMapper;
    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    AddGoodsMapper addGoodsMapper;

    @Override
    public BaseData queryGrouponList(Integer page, Integer limit, String sort, String order, String goodsId) {
        //预编译的时候增加分页的sql语句
        PageHelper.startPage(page, limit);
        //设置排序的条件
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        grouponRulesExample.setOrderByClause(sort + " " + order);

        if (goodsId != null && !"".equals(goodsId)) {
            try {
                int id = Integer.parseInt(goodsId);
                grouponRulesExample.createCriteria().andGoodsIdEqualTo(id);
            } catch (Exception e) {
                grouponRulesExample.createCriteria().andGoodsIdEqualTo(0);            }
        }
        //获取查询结果
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);
        //可以在查询完成后获得pageInfo → 根据查询结果获得总条目数total
        PageInfo<GrouponRules> adPageInfo = new PageInfo<>(grouponRules);
        long total = adPageInfo.getTotal();

        //所需数据封装并返回
        return BaseData.data(grouponRules, total);
    }

    @Override
    public BaseData queryRecordList(Integer page, Integer limit, String sort, String order) {
        //预编译的时候增加分页的sql语句
        PageHelper.startPage(page, limit);
        //设置排序的条件
        GrouponExample grouponExample = new GrouponExample();
        grouponExample.setOrderByClause(sort + " " + order);
        //获取查询结果
        List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);
        //可以在查询完成后获得pageInfo → 根据查询结果获得总条目数total
        PageInfo<Groupon> adPageInfo = new PageInfo<>(groupons);
        long total = adPageInfo.getTotal();

        //所需数据封装并返回
        return BaseData.data(groupons, total);
    }

    @Override
    public void updateGrouponRules(GrouponRules grouponRules) {
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        grouponRulesExample.createCriteria().andIdEqualTo(grouponRules.getId());
        grouponRulesMapper.updateByExample(grouponRules,grouponRulesExample);
    }


    @Override
    public void deleteGrouponRules(GrouponRules grouponRules) {
        grouponRulesMapper.deleteByPrimaryKey(grouponRules.getId());
    }

    @Override
    public GrouponRules createGrouponRules(GrouponRules grouponRules) {
        AddGoods addGoods = addGoodsMapper.selectByPrimaryKey(grouponRules.getGoodsId());
        // TODO: 2020/11/3 用goodsId查询goods信息并赋值到grouponRules
        grouponRules.setAddTime(new Date());
        grouponRules.setDeleted(false);
        grouponRules.setUpdateTime(new Date());
        grouponRules.setGoodsName(addGoods.getName());
        grouponRules.setPicUrl(addGoods.getPicUrl());
        grouponRulesMapper.insert(grouponRules);

        return grouponRules;


    }

}
